import 'package:backend.core/core/domain/entities/api/web/yookassa/api_yookassa_one_time_purchased_item_transaction.dart';
import 'package:backend.core/core/domain/entities/api/web/yookassa/api_yookassa_transaction_revocation_info.dart';
import 'package:backend.core/core/domain/entities/web/yookassa/yookassa_one_time_purchased_item_transaction.dart';
import 'package:backend.purchases/domain/helpers/api/create_api_money_amount_from_internal_one.dart';
import 'package:backend.purchases/domain/helpers/api/web/create_api_web_one_time_purchased_item_transaction_offer_type_from_internal_one.dart';
import 'package:backend.purchases/domain/helpers/api/web/yookassa/create_api_yookassa_transaction_revocation_info_from_internal_one.dart';

abstract class CreateApiYookassaOneTimePurchasedItemTransactionFromInternalOne {
  ApiYookassaOneTimePurchasedItemTransaction call({
    required YookassaOneTimePurchasedItemTransaction transaction,
  });
}

class CreateApiYookassaOneTimePurchasedItemTransactionFromInternalOneImpl
    implements CreateApiYookassaOneTimePurchasedItemTransactionFromInternalOne {
  const CreateApiYookassaOneTimePurchasedItemTransactionFromInternalOneImpl({
    required CreateApiMoneyAmountFromInternalOne createApiMoneyAmountFromInternalOne,
    required CreateApiYookassaTransactionRevocationInfoFromInternalOne
        createApiYookassaTransactionRevocationInfoFromInternalOne,
    required CreateApiWebOneTimePurchasedItemTransactionOfferTypeFromInternalOne
        createApiWebOneTimePurchasedItemTransactionOfferTypeFromInternalOne,
  })  : _createApiMoneyAmountFromInternalOne = createApiMoneyAmountFromInternalOne,
        _createApiYookassaTransactionRevocationInfoFromInternalOne =
            createApiYookassaTransactionRevocationInfoFromInternalOne,
        _createApiWebOneTimePurchasedItemTransactionOfferTypeFromInternalOne =
            createApiWebOneTimePurchasedItemTransactionOfferTypeFromInternalOne;

  final CreateApiMoneyAmountFromInternalOne _createApiMoneyAmountFromInternalOne;
  final CreateApiYookassaTransactionRevocationInfoFromInternalOne
      _createApiYookassaTransactionRevocationInfoFromInternalOne;
  final CreateApiWebOneTimePurchasedItemTransactionOfferTypeFromInternalOne
      _createApiWebOneTimePurchasedItemTransactionOfferTypeFromInternalOne;

  @override
  ApiYookassaOneTimePurchasedItemTransaction call({
    required YookassaOneTimePurchasedItemTransaction transaction,
  }) {
    final apiAmount = _createApiMoneyAmountFromInternalOne(
      moneyAmount: transaction.amount,
    );

    final apiOfferType = _createApiWebOneTimePurchasedItemTransactionOfferTypeFromInternalOne(
      offerType: transaction.offerType,
    );

    ApiYookassaTransactionRevocationInfo? apiRevocationInfo;

    if (transaction.revocationInfo != null) {
      apiRevocationInfo = _createApiYookassaTransactionRevocationInfoFromInternalOne(
        revocationInfo: transaction.revocationInfo!,
      );
    }

    final apiIncomeAmount = _createApiMoneyAmountFromInternalOne(
      moneyAmount: transaction.incomeAmount,
    );

    return ApiYookassaOneTimePurchasedItemTransaction(
      id: transaction.id,
      amount: apiAmount,
      offerType: apiOfferType,
      time: transaction.time,
      revocationInfo: apiRevocationInfo,
      incomeAmount: apiIncomeAmount,
    );
  }
}
